import 'package:custompaint/bean/WidgetType.dart';
import 'package:custompaint/widget/CustomTextField.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';

Future<WidgetType?> showSelectWidgetDialog() async {
  return await Get.dialog(Container(
    width: Get.width * 0.1,
    height: Get.height * 0.1,
    margin: EdgeInsets.symmetric(
        vertical: Get.height * 0.1, horizontal: Get.width * 0.1),
    decoration: const BoxDecoration(color: Colors.white),
    child: Column(
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            InkWell(
              onTap: Get.back,
              child: const Padding(
                padding: EdgeInsets.all(8.0),
                child: Icon(Icons.close),
              ),
            )
          ],
        ),
        Expanded(
          child: Center(
            child: Wrap(
              alignment: WrapAlignment.center,
              children: List.generate(WidgetType.values.length, (index) {
                var bean = WidgetType.values[index];
                return InkWell(
                  onTap: () {
                    Get.back(result: bean);
                  },
                  child: Container(
                      padding: const EdgeInsets.symmetric(
                          horizontal: 25, vertical: 10),
                      margin: const EdgeInsets.all(10),
                      decoration: BoxDecoration(
                          color: Colors.blue,
                          borderRadius: BorderRadius.circular(20)),
                      child: Text(
                        bean.name,
                        style: const TextStyle(color: Colors.white),
                      )),
                );
              }),
            ),
          ),
        ),
      ],
    ),
  ));
}

Future<WidgetType?> showCode(String code) async {
  return await Get.dialog(Container(
      width: Get.width * 0.1,
      height: Get.height * 0.1,
      margin: EdgeInsets.symmetric(
          vertical: Get.height * 0.1, horizontal: Get.width * 0.1),
      padding: const EdgeInsets.all(10),
      decoration: const BoxDecoration(color: Colors.white),
      child: Column(children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            InkWell(
              onTap: Get.back,
              child: const Padding(
                padding: EdgeInsets.all(8.0),
                child: Icon(Icons.close),
              ),
            )
          ],
        ),
        Expanded(
          child: CustomTextField(
            value: code,
          ),
        ),
        ElevatedButton(
            onPressed: () {
              Clipboard.setData(ClipboardData(text: code));
              Get.back();
            },
            child: const Text("复制并关闭")),
      ])));
}

Future<String?> showInputDialog(InputDecoration inputDecoration,
    {List<TextInputFormatter>? inputFormatter, String? value}) async {
  return await Get.dialog(GetBuilder(
    init: InputDialogLogic(value),
    assignId: true,
    builder: (logic) {
      return Container(
          width: Get.width * 0.1,
          height: Get.height * 0.1,
          margin: EdgeInsets.symmetric(
              vertical: Get.height * 0.3, horizontal: Get.width * 0.3),
          padding: const EdgeInsets.all(10),
          decoration: const BoxDecoration(color: Colors.white),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  InkWell(
                    onTap: Get.back,
                    child: const Padding(
                      padding: EdgeInsets.all(8.0),
                      child: Icon(Icons.close),
                    ),
                  )
                ],
              ),
              Expanded(
                child: Row(
                  children: [
                    Expanded(
                      child: Obx(() {
                        return CustomTextField(
                          value: logic.text.value,
                          inputFormatters: inputFormatter,
                          inputDecoration: inputDecoration,
                          textAlign: TextAlign.center,
                          onChanged: (t) {
                            logic.text.value = t;
                          },
                        );
                      }),
                    ),
                    ElevatedButton(
                        onPressed: () {
                          Get.back(result: logic.text.value);
                        },
                        child: const Text("确定"))
                  ],
                ),
              )
            ],
          ));
    },
  ));
}

class InputDialogLogic extends GetxController {
  var text = "".obs;

  String? value;

  InputDialogLogic(this.value);
  @override
  void onInit() {
    super.onInit();
    text.value = value ?? "";
  }
}
